package com.dede.getmusic.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import org.jetbrains.anko.db.*

/**
 * @author hsh
 * @date 2017/10/13 9:23.
 * @doc
 */
class MusicDbOpenHelper(context: Context) : ManagedSQLiteOpenHelper(context, "GeiMusic", null, 1) {

    class SearchHis {
        companion object {
            val SEARCH_HIS_TABLE = "search_his"
            val SEARCH_KEY = "searchKey"
            val DATE = "date"
        }
    }

    override fun onCreate(db: SQLiteDatabase) {
        db.createTable(
                SearchHis.SEARCH_HIS_TABLE, true,
                "id" to INTEGER + PRIMARY_KEY,
                SearchHis.SEARCH_KEY to TEXT,
                SearchHis.DATE to SqlTimeType("TIMESTAMP NOT NULL DEFAULT (strftime('%s','now','localtime'))")
        )
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
    }

    private class SqlTimeType(override val name: String) : SqlType {

        override fun render(): String {
            return name
        }

        override fun plus(m: SqlTypeModifier): SqlType {
            return SqlTimeType(name)
        }

    }
}